<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/20
 * Time: 06:01
 */

namespace HiCommon\Model;

class Goods extends Base
{

    protected $table = 'goods';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates = [
    ];

    //一对一关联分类
    public function cate()
    {
        return $this->hasOne(GoodsCate::class, 'id', 'cate_id')
            ->select('id', 'name');
    }

    //一对多
    public function product()
    {
        return $this->hasMany(GoodsProduct::class, 'goods_id', 'id')
            ->with(['specs' => function ($query) {
                $query->select('id', 'name', 'goods_specs_cate_id');
            }]);
    }

    //一对多
    public function product_info()
    {
        return $this->hasMany(GoodsProduct::class, 'goods_id', 'id')
            ->select('id', 'goods_id', 'price', 'integral', 'stock')
            //->where('stock', '>', 0)
            ->orderBy('integral', 'asc');
    }

    //一对多
    public function img()
    {
        return $this->hasMany(GoodsImg::class, 'goods_id', 'id')
            ->select('id', 'goods_id', 'picurl');
    }


    //一对一关联分类
    // hinq 2019 09 09
    public function shop()
    {
        return $this->hasOne(Shop::class, 'id', 'repairstore_id');
    }

    public function shop_name()
    {
        return $this->hasOne(Shop::class, 'id', 'repairstore_id')
            ->select(['id', 'name']);
    }

    //一对多关联商品服务标签关系表
    public function sale_tag_relation()
    {
        return $this->hasMany(GoodsSaleTagRelation::class, 'goods_id', 'id')
            ->select('id', 'goods_id', 'sale_tag_id');
    }


    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value) {
            case 0:
                $res = ['id' => 0, 'name' => '草稿'];
                break;
            case 1:
                $res = ['id' => 1, 'name' => '待审核'];
                break;
            case 2:
                $res = ['id' => 2, 'name' => '通过'];
                break;
            case 3:
                $res = ['id' => 3, 'name' => '拒绝'];
                break;
            default:
                $res = ['id' => -1, 'name' => '未知'];
        }
        return $res;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getPicUrlAttribute($value)
    {
        $arr_data = [
            'src' => $value,
            'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        $s_created_at = $this->created_at;
        $arr_data = [
            'created_at' => ymd_friendly_date($s_created_at),
        ];

        return $arr_data;
    }


}